package com.george.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.george.common.entity.dto.user.ScbUserDTO;
import com.george.user.dao.ScbUserDAO;
import com.george.user.mapper.ScbUserMapper;
import com.george.user.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Create By George on /2023/4/10
 * description: 用户服务接口实现
 *
 * @author George
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    private final ScbUserMapper scbUserMapper;

    /**
     * 根据用户id获取用户信息
     *
     * @param userId
     * @return com.george.user.entity.dto.ScbUserDTO
     * @author George
     * @date 2023/4/12 17:02
     */

    @Override
    public ScbUserDTO getUserInfoById(long userId) {
        log.info("获取用户信息，用户id：{}", userId);
        ScbUserDTO scbUserDTO = new ScbUserDTO();
        ScbUserDAO scbUserDAO = scbUserMapper.selectById(userId);
        if (Objects.nonNull(scbUserDAO)) {
            BeanUtils.copyProperties(scbUserDAO, scbUserDTO);
        }
        System.out.println("获取用户信息成功，用户信息：" + scbUserDAO);
        return scbUserDTO;
    }


    /**
     * 根据用户名称获取用户信息
     *
     * @param name
     * @return com.george.user.entity.dto.ScbUserDTO
     * @author George
     * @date 2023/4/12 17:02
     */

    @Override
    public ScbUserDTO getUserInfoByName(String name) {
        log.info("获取用户信息，用户名称：{}", name);
        ScbUserDTO scbUserDTO = null;
        LambdaQueryWrapper<ScbUserDAO> scbUserDAOLambdaQueryWrapper = new LambdaQueryWrapper<>();
        scbUserDAOLambdaQueryWrapper.eq(ScbUserDAO::getName, name);
        ScbUserDAO scbUserDAO = scbUserMapper.selectOne(scbUserDAOLambdaQueryWrapper);
        if (Objects.nonNull(scbUserDAO)) {
            scbUserDTO = new ScbUserDTO();
            BeanUtils.copyProperties(scbUserDAO, scbUserDTO);
        }
        log.info("获取用户信息成功，用户信息：" + scbUserDAO);
        return scbUserDTO;
    }

    @Override
    public ScbUserDTO getUserInfoByUsername(String username) {
        log.info("获取用户信息，登录用户名称：{}", username);
        ScbUserDTO scbUserDTO = null;
        LambdaQueryWrapper<ScbUserDAO> scbUserDAOLambdaQueryWrapper = new LambdaQueryWrapper<>();
        scbUserDAOLambdaQueryWrapper.eq(ScbUserDAO::getUsername, username);
        ScbUserDAO scbUserDAO = scbUserMapper.selectOne(scbUserDAOLambdaQueryWrapper);
        if (Objects.nonNull(scbUserDAO)) {
            scbUserDTO = new ScbUserDTO();
            BeanUtils.copyProperties(scbUserDAO, scbUserDTO);
        }
        log.info("获取用户信息成功，用户信息：" + scbUserDAO);
        return scbUserDTO;
    }

    /**
     * 更新用户信息
     *
     * @param scbUserDTO scb用户dto
     * @return {@link ScbUserDTO}
     */
    @Override
    public ScbUserDTO updateUserInfoById(ScbUserDTO scbUserDTO) {
        log.info("更新用户信息，用户：{}", JSON.toJSONString(scbUserDTO));
        ScbUserDAO scbUserDAO = new ScbUserDAO();
        BeanUtils.copyProperties(scbUserDTO,scbUserDAO);
        scbUserMapper.updateById(scbUserDAO);
        log.info("更新用户信息成功，用户信息：" + scbUserDAO);
        return scbUserDTO;
    }
}
